package cn.myjerry.io.others;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * 
 * @author Jerry
 *
 */

public class ObjectDemo01 {
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		// seri("/Users/zpon/Desktop/test03.txt");
		read("/Users/zpon/Desktop/test03.txt");
	}
	// 反序列化
	public static void read(String destPath) throws IOException, ClassNotFoundException {
		// 创建源
		File dest = new File(destPath);
		// 选择流
		ObjectInputStream ois = new ObjectInputStream(
				new BufferedInputStream(
						new FileInputStream(dest)
						)
				);
		Object obj = ois.readObject();
		if(obj instanceof Employee) {
			Employee emp = (Employee) obj;
			System.out.println(emp.getSalary());
		}
		
		ois.close();
	}
	// 序列化
	public static void seri(String destPath) throws IOException {
		Employee emp = new Employee("Jerry", 10000);
		// 创建源
		File dest = new File(destPath);
		// 选择流
		ObjectOutputStream oos = new ObjectOutputStream(
				new BufferedOutputStream(
						new FileOutputStream(dest)
						)
				);
		oos.writeObject(emp);
		oos.flush();
		
		oos.close();
	}
}
